﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Web;
using System.Web.Mvc;
 
using LY.Common;
using LY.Common.Utils;
 
using QJ.MobileWeb.Models;
using System.Text.RegularExpressions;

namespace QJ.MobileWeb.Controllers
{
    public class DefaultController : Controller
    {
        //
        // GET: /Default/

        public ActionResult Index()
        {
            //string msg = "";
            //SmsMessage.Send("13818609394", "[全局金福] 新年快乐", 1, out msg);
            //调用接口实例
            //string md5Password = MD5Util.GetMD5_32("123456"); 
            // var content = new FormUrlEncodedContent(new[] 
            //{
            //    new KeyValuePair<string, string>("Phone", "13818609394"),
            //    new KeyValuePair<string, string>("Password", md5Password)
            //}); 
            //var jsonData = ApiRepository.Post("Member/Login.do", content); 
            //return Json(jsonData,JsonRequestBehavior.AllowGet);
            return View();
        }

        public ActionResult Register()
        {
            return View();

        }

        [HttpPost]
        public ActionResult Register(FormCollection collection)
        {
            if (!string.IsNullOrEmpty(collection["phone"]))
            {
                //定义手机号验证规则
                string dianxin = @"^1[3578][01379]\d{8}$";
                string liantong = @"^1[34578][01256]\d{8}$";
                string yidong = @"^(134[0-8]\d{7}|1[34578][012356789]\d{8})$";
                Regex dReg = new Regex(dianxin);
                Regex lReg = new Regex(liantong);
                Regex yReg = new Regex(yidong);
                if (dReg.IsMatch(collection["phone"]) || lReg.IsMatch(collection["phone"]) || yReg.IsMatch(collection["phone"]))
                {
                    if (!string.IsNullOrEmpty(collection["pwd"]))
                    {
                        string md5Password = MD5Util.GetMD5_32(collection["pwd"]);
                        var content = new FormUrlEncodedContent(new[] 
                    {
                        new KeyValuePair<string, string>("Phone", collection["phone"]),
                        new KeyValuePair<string, string>("Password", md5Password) 
                    });
                        var jsonData = ApiRepository<string>.Post("Member/Register.do", content);   
                        return Json(jsonData, JsonRequestBehavior.AllowGet);
                    }
                    else
                    {
                        return Content("-1");
                    }
                }
                else
                {
                    return Content("-2");
                }
            }
            else
            {
                return Content("-3");
            }
        }


        public ActionResult Success()
        {
            return View();
        }

    }
}
